﻿(async () => {
    await window.ris.initialize();

    $(() => {
        const $eventEmitter = $('body > .container');
        let reloadGrid = null;
        $('.main').splitter({
            orientation: 'v',
            panel1Size: '300px',
        });
        const quickFilter = $('.quick-filter').quickFilter(
            `/api/filter/get-filter-by-type/exam`,
            filterId => {
                reloadGrid = () => {
                    dataGrid.reload({ filterId });
                };
                reloadGrid();
            },
            {
                type: 'GET',
            }
        );

        const advancedSearch = $('.advanced-search').advancedSearchWithCollapse(null, (e) => {
            reloadGrid = () => {
                dataGrid.refresh({ userData: e });
            };
            reloadGrid();
        });
        const gridOperations = [];
        const gridContextMenuItems = [];
        const dataGrid = $('.exam-grid').dataGrid({
            key: 'oid',
            getOperations() {
                return gridOperations;
            },
            getContextMenuItems() {
                return gridContextMenuItems;
            },
            onLoad(loadOptions) {
                return new Promise((resolve, reject) => {
                    if (loadOptions.filterId || (loadOptions.userData && !$.isEmptyObject(loadOptions.userData))) {
                        $.post({
                            dataType: 'json',
                            contentType: 'application/json;charset=UTF-8',
                            url: `/api/report/get-list`,
                            data: JSON.stringify(loadOptions),
                            success: result => {
                                if (result.success) {
                                    resolve(result)
                                } else {
                                    reject(result.error);
                                }
                            }
                        });
                    } else {
                        resolve({ success: true, data: { columns: [], rows: [], totalCount: 0 } })
                    }
                })
            },
        });

        const tools = $.getReportTools($eventEmitter, quickFilter, advancedSearch, dataGrid, null, null, null, {
            reloadGrid: () => {
                if (reloadGrid instanceof Function) {
                    reloadGrid();
                }
            }
        });

        const toolbar = $('.toolbar').dxToolbar({}).dxToolbar('instance');
        //判断是否已经启用了自动刷新
        tools.autoRefresh.options.text = autoRefresh('reportRefreshInterval', 'reportInterval', () => {
            if (reloadGrid instanceof Function) {
                reloadGrid();
            }
        }, true);
        tools.refreshSetting.options.items = [tools.autoRefresh.options, tools.refreshInterval.options]
        //设置打印
        tools.print.options.items = [tools.formalPrint.options, tools.temporarilyPrint.options];
        toolbar.option('items', [tools.write, tools.audit, tools.open, tools.viewImage, tools.print, tools.collectTo, tools.qualityControl, tools.refresh, tools.refreshSetting]);
        gridOperations.push(tools.open, tools.viewImage, tools.write, tools.audit, tools.collectTo);
        gridContextMenuItems.push(tools.open, tools.viewImage, tools.write, tools.audit, tools.print, tools.markFollowUp,
            tools.followUpRecord, tools.revisit, tools.eApply, tools.eMedical, tools.collectTo);
        $.get('/api/report/get-advanced-search-view').then(result => {
            if (result.success) {
                if (result.view) {
                    if (typeof result.view === 'string') {
                        advancedSearch.updateView(JSON.parse(result.view), result.data);
                    } else {
                        advancedSearch.updateView(result.view, result.data);
                    }
                }
            }
        });
    });
})();